using System;
using System.Collections.Generic;

namespace Publish.Services.SocketUdp
{
    /// <summary>
    /// 将未收到的信息放到静态队列中
    /// </summary>
    public class QueueMessage
    {
        #region Property

        public static IDictionary<String, Queue<MessageInfo>> idictMessage = new Dictionary<String, Queue<MessageInfo>>();

        /// <summary>
        /// 存放信息队列的数据字典
        /// </summary>
        public static IDictionary<String, Queue<MessageInfo>> IdictMessage
        {
            get { return idictMessage; }
        }

        #endregion

        #region Method

        /// <summary>
        /// 增加 主要将信息加入到队里字典中
        /// </summary>
        /// <param name="host">信息来源的主机</param>
        /// <param name="messageInfo">信息的实体</param>
        public static void Add(String host, MessageInfo messageInfo)
        {
            if (idictMessage.Keys.Contains(host))
            {
                //将信息加入原来的队列
                idictMessage[host].Enqueue(messageInfo);
            }
            else
            {
                //新建一个键  并将信息入队
                var queue = new Queue<MessageInfo>();
                queue.Enqueue(messageInfo);
                idictMessage.Add(host, queue);
            }
        }

        /// <summary>
        /// 得到相应主机的信息队列
        /// </summary>
        /// <param name="host">主机信息</param>
        /// <returns>返回一个队列信息</returns>
        public static Queue<MessageInfo> GetAndRemove(String host)
        {
            Queue<MessageInfo> queue = null;

            if (idictMessage.Keys.Contains(host))
            {
                //取出并移除字典
                queue = idictMessage[host];
                idictMessage.Remove(host);
            }

            return queue;
        }

        #endregion
    }
}
